package com.qcxk.yuqian.system.service.imp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.system.component.QueryWrapperConverter;
import com.qcxk.yuqian.system.entity.Dept;
import com.qcxk.yuqian.system.entity.User;
import com.qcxk.yuqian.system.mapper.DeptMapper;
import com.qcxk.yuqian.system.service.IDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcxk.yuqian.system.utils.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @auther lp
 * @create 2021-05-24 18:35:50
 * @describe 部门表服务实现类
 */
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private RedisUtil redisUtil;


    @Override
    public Page queryDeptPage(Page page, Dept dept) {

        QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
        QueryWrapperConverter.converter(deptQueryWrapper, dept);

        deptQueryWrapper.orderByDesc("ctime");
        deptMapper.selectPage(page, deptQueryWrapper);
        return page;
    }

    @Override
    public boolean deleteDeptById(Dept dept) {
        int re = deptMapper.deleteById(dept.getId());
        return re == 1 ? true : false;
    }

    @Override
    public List<Dept> getChildList(String id, String isContainUser) {
        List<Dept> deptList = deptMapper.selectChildList(id);
        if (deptList != null & !deptList.isEmpty()) {
            if (StringUtils.isNotBlank(isContainUser)) {
                if (isContainUser.equals("0")) {//包含用户
                    deptList.stream().forEach(e -> {
                        List<User> tempList = new ArrayList<>();
                        JSONArray jsonArray = new JSONArray();
                        for (int i = 0; i < tempList.size(); i++) {
                            User item = tempList.get(i);
                            JSONObject json = new JSONObject();
                            json.put("id", item.getId());
                            json.put("departName", item.getFullName());
                            json.put("parentId", e.getParentId());
                            json.put("type", "user");
                            jsonArray.add(json);
                        }
                        e.setUserList(jsonArray);
                    });
                }
            }
            deptList = getTree(deptList, Integer.parseInt(id));
        }
        return deptList;
    }

    private List<Dept> getTree(List<Dept> list, int parentId) {
        //获取所有子节点
        List<Dept> childTreeList = getChildTree(list, parentId);
        for (Dept dept : childTreeList) {
            dept.setChildList(getTree(list, dept.getId()));
        }
        return childTreeList;
    }

    private List<Dept> getChildTree(List<Dept> list, int id) {
        List<Dept> childTree = new ArrayList<>();
        for (Dept dept : list) {
            if (dept.getParentId() == id) {
                childTree.add(dept);
            }
        }
        return childTree;
    }

    @Override
    public List<Dept> getParentList(String id, String isContainUser) {
        return deptMapper.selectParentList(id);
    }
}
